package com.taoyibao.hongcanting.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.taoyibao.hongcanting.ui.HongApplication;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 16:46
 */
public class AppUtils {
    /**
     * 获取应用名称
     *
     * @return
     */
    public static String getAppName() {
        try {
            PackageManager packageManager = HongApplication.getContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    HongApplication.getContext().getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            String packageName = packageInfo.packageName;
            LogUtils.dNormal("包名", packageName);
            return HongApplication.getContext().getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取应用包名
     *
     * @return
     */
    public static String getPackageName() {
        try {
            PackageManager packageManager = HongApplication.getContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(HongApplication.getContext().getPackageName(), 0);

            String packageName = packageInfo.packageName;

            return packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取版本号
     *
     * @return
     */
    public static int getVersonCode() {
        int versionCode = 0;
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = HongApplication.getContext().getPackageManager().
                    getPackageInfo(HongApplication.getContext().getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取版本名称
     *
     * @return
     */
    public static String getVersionName() {
        String verName = "";
        try {
            verName = HongApplication.getContext().getPackageManager().
                    getPackageInfo(HongApplication.getContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    /**
     * 判断当前手机是否能够使用DownloadManager
     *
     * @return true 可以使用  false 不可以使用
     */
    public static boolean canDownloadState() {
        try {
            int state = HongApplication.getContext().getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");

            if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
                    || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
